<template>
  <div
    class="mask"
    v-if="showCart && amount > 0"
    @click="handleCartShowChange"
  ></div>
  <div class="cart">
    <!-- 购物车列表 -->
    <div class="product" v-show="showCart && amount > 0">
      <!-- 顶部栏 -->
      <div class="product__header">
        <div
          class="product__header__all"
          @click="() => setCartAllChecked(shopId, selectALL)"
        >
          <span
            class="product__header__icon iconfont"
            v-html="selectALL ? '&#xe652;' : '&#xe607;'"
          ></span>
          全选
        </div>
        <div
          class="product__header__clear"
          @click="() => cleanCartProduct(shopId)"
        >
          清空购物车
        </div>
      </div>
      <!-- 购物车 -->
      <template v-for="item in shopList" :key="item._id">
        <!-- 购物车信息 -->
        <div class="product__item" v-if="item.count > 0">
          <!-- check -->
          <div
            class="product__item__checked iconfont"
            v-html="item.check ? '&#xe652;' : '&#xe607;'"
            @click="changeCartItemCheck(shopId, item._id)"
          ></div>
          <!-- img -->
          <img class="product__item__img" :src="item.imgUrl" />
          <!-- 信息 -->
          <div class="product__item__detail">
            <h4 class="product__item__title">{{ item.name }}</h4>
            <p class="product__item__price">
              <span class="product__item__yen">&yen;</span>{{ item.price }}
              <span class="product__item__origin"
                >&yen;{{ item.oldPrice }}</span
              >
            </p>
          </div>
          <!-- - + -->
          <div class="product__number">
            <span
              class="product__number__minus"
              @click="
                () => {
                  changeCartItemInfo(shopId, item._id, item, -1);
                }
              "
              >-</span
            >
            {{ item.count || 0 }}
            <span
              class="product__number__plus"
              @click="
                () => {
                  changeCartItemInfo(shopId, item._id, item, 1);
                }
              "
              >+</span
            >
          </div>
        </div>
      </template>
    </div>

    <!-- 底部结算  -->
    <div class="check">
      <div class="check__icon">
        <img
          class="check__icon__img"
          src="http://www.dell-lee.com/imgs/vue3/basket.png"
          alt=""
          @click="handleCartShowChange"
        />
        <div class="check__icon__tag">{{ amount }}</div>
      </div>
      <!--  -->
      <div class="check__amount">
        总计:<span class="check__amount__price">&yen;{{ totalPrice }}</span>
      </div>
      <div class="check__btn" v-show="amount">
        <router-link :to="{ path: `/orderConfirm/${shopId}` }">
          去结算
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, ref } from 'vue';
import { useStore } from 'vuex';
import { useRoute } from 'vue-router';
import { useCommonCartEffect } from '../../effects/cartEffects';

const useCartEffect = (shopId) => {
  const store = useStore();

  const { cartList, shopList, totalPrice, changeCartItemInfo } =
    useCommonCartEffect(shopId);
  //#region
  // const cartList = store.state.cartList;
  //  calculations
  // const calculations = computed(() => {
  //   const result = { count: 0, price: 0, selectALL: true };
  //   const productList = cartList[shopId]?.productList;
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i];
  //       result.count += product.count;
  //       if (product.price) {
  //         result.price += product.price * product.count;
  //       }
  //       if (product.count > 0 && !product.check) {
  //         result.selectALL = false;
  //       }
  //     }
  //   }
  //   result.price = result.price.toFixed(2);
  //   return { result };
  // });
  //#endregion

  // 计算--总数
  const amount = computed(() => {
    const productList = cartList[shopId]?.productList;
    let count = 0;
    if (productList) {
      for (const i in productList) {
        const product = productList[i];
        count += product.count;
      }
    }
    return count;
  });
  // 计算--总价
  // const totalPrice = computed(() => {
  //   const productList = cartList[shopId]?.productList;
  //   let price = 0;
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i];
  //       if (product.check) {
  //         price += product.count * product.price;
  //       }
  //     }
  //   }
  //   return price.toFixed(2);
  // });
  // 计算--全选
  const selectALL = computed(() => {
    const productList = cartList[shopId]?.productList;
    let result = true;
    if (productList) {
      for (const i in productList) {
        const product = productList[i];
        if (product.count > 0 && !product.check) {
          result = false;
        }
      }
    }
    return result;
  });

  // 计算--购物车列表
  // const shopList = computed(() => {
  //   const productList = cartList[shopId]?.productList || [];
  //   return productList;
  // });

  // 是否勾选
  const changeCartItemCheck = (shopId, productId) => {
    store.commit('changeCartItemChecked', {
      shopId,
      productId,
    });
  };
  // 是否全选
  const setCartAllChecked = (shopId, selectALL) => {
    store.commit('setCartAllChecked', { shopId, selectALL });
  };
  // 清空购物车
  const cleanCartProduct = (shopId) => {
    store.commit('cleanCartProduct', { shopId });
    // handleCartShowChange();
  };

  return {
    amount,
    totalPrice,
    shopList,
    selectALL,
    changeCartItemInfo,
    changeCartItemCheck,
    cleanCartProduct,
    setCartAllChecked,
  };
};

const toggleCartEffect = () => {
  const showCart = ref(false);
  // 是否展示购物车
  const handleCartShowChange = () => {
    showCart.value = !showCart.value;
  };

  return { showCart, handleCartShowChange };
};

export default {
  name: 'Cart',
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const { showCart, handleCartShowChange } = toggleCartEffect();

    const {
      amount,
      totalPrice,
      shopList,
      selectALL,
      changeCartItemInfo,
      changeCartItemCheck,
      cleanCartProduct,
      setCartAllChecked,
    } = useCartEffect(shopId);

    return {
      amount,
      totalPrice,
      shopList,
      shopId,
      selectALL,
      showCart,
      changeCartItemInfo,
      changeCartItemCheck,
      cleanCartProduct,
      setCartAllChecked,
      handleCartShowChange,
    };
  },
};
</script>

<style lang="scss" scoped>
@import '../../style/variables.scss';
@import '../../style/mixins.scss';
.mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba($color: #000000, $alpha: 0.5);
  z-index: 1;
}
.cart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 -1px 1px 0 $content-bgColor;
  z-index: 10;
  background: $bgColor;
}
.product {
  flex: 1;
  overflow-y: scroll;
  background-color: $bgColor;
  &__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: calc(100% - 32px);
    margin: 0 auto;
    line-height: 0.52rem;
    border-bottom: 0.01rem solid $content-bgColor;
    font-size: 0.14rem;
    color: $content-fontColor;
    &__all {
      width: 0.64rem;
    }
    &__icon {
      display: inline-block;
      vertical-align: top;
      color: $btn-bgColor;
      font-size: 0.2rem;
      margin-right: 0.1rem;
    }
    &__clear {
      cursor: pointer;
    }
  }
  &__item {
    position: relative;
    display: flex;
    align-items: center;
    // justify-content: space-between;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;
    &__checked {
      line-height: 0.5rem;
      margin-right: 0.2rem;
      color: $btn-bgColor;
      font-size: 0.2rem;
    }
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-right: 0.16rem;
    }
    &__title {
      margin: 0.06rem 0 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;
      @include ellipsis;
    }

    &__price {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $highlight-fontColor;
    }
    &__yen {
      font-size: 0.12rem;
    }
    &__origin {
      margin-left: 0.06rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
    // number
    .product__number {
      position: absolute;
      right: 0;
      bottom: 0.26rem;
      // width: 0.8rem;
      &__minus,
      &__plus {
        display: inline-block;
        width: 0.2rem;
        height: 0.2rem;
        line-height: 0.16rem;
        border-radius: 50%;
        font-size: 0.2rem;
        text-align: center;
      }
      &__minus {
        border: 0.01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: 0.05rem;
      }
      &__plus {
        background-color: $btn-bgColor;
        color: $bgColor;
        margin-left: 0.05rem;
      }
    }
  }
}
.check {
  display: flex;
  line-height: 0.5rem;
  height: 0.5rem;
  border-top: 0.01 solid $content-bgColor;
  // background-color: slateblue;
  &__icon {
    position: relative;
    width: 0.76rem;

    &__img {
      width: 0.28rem;
      height: 0.26rem;
      display: block;
      margin: 0.12rem auto;
    }
    &__tag {
      position: absolute;
      top: 0.04rem;
      left: 0.45rem;
      padding: 0 0.04rem;
      min-width: 0.2rem;
      height: 0.2rem;
      line-height: 0.2rem;
      border-radius: 0.1rem;
      background-color: $highlight-fontColor;
      font-size: 0.12rem;
      text-align: center;
      color: $bgColor;
      transform: scale(0.7);
      transform-origin: left center;
    }
  }
  &__amount {
    flex: 1;
    color: $content-fontColor;
    font-size: 0.12rem;
    margin: 0 0.08rem;
    &__price {
      color: $highlight-fontColor;
      font-size: 0.18rem;
      margin: 0 0.08rem;
    }
  }
  &__btn {
    display: block;
    width: 0.98rem;
    background-color: #4fb0f9;
    font-size: 0.14rem;
    text-align: center;
    a {
      text-decoration: none;
      color: $bgColor;
    }
  }
}
</style>
